

library(sp)
library(rgdal)
library(foreign)
library(ggplot2)
library(dplyr)
library(RColorBrewer)
library(ggmap)
library(rgeos)
library(maptools)
library(scales)
library(extrafont)
library(gridExtra)
rm(list = ls())
font_import()
loadfonts(device = "win")

setwd("/Users/linma478/Dropbox (IPL)/SwissGeoAssign")


x.shp <- readShapeSpatial("Maps/Swiss/ch-cantons.shp")
class(x.shp)
cant <- read.dta("Data/cant_map.dta")
x.shp.f <- fortify(x.shp, region = "ID_1")
merge_all <- merge(x.shp.f, cant, by="id", all.x=TRUE)
merge_all <- merge_all[order(merge_all$order), ] 


######## set colors ########

colb2 <-  c("#C6DBEF" ,"#9ECAE1", "#6BAED6", "#4292C6","#2171B5" ,"#084594","#08306B")


########################
##     set breaks  ####
#######################


summary(merge_all$iraq_all)

merge_all$b_af <- cut(merge_all$afghanistan_all, 
                      breaks=c(-1,50,100,300,500,700,1000), 
                      labels=c("50","100", "300", "500","700","1000"))
merge_all$b_iran <- cut(merge_all$iran_all, 
                      breaks=c(-1,50,100,300,500,700,1000), 
                      labels=c("50","100", "300", "500","700","1000"))
merge_all$b_et <- cut(merge_all$ethiopia_all, 
                      breaks=c(-1,50,100,200,300,400, 605), 
                      labels=c("50", "100", "200","300","400","600"))
merge_all$b_sy <- cut(merge_all$syria_all, 
                      breaks=c(-1,50,100,200,300,400,605), 
                      labels=c("50", "100", "200","300","400","600"))
merge_all$b_iraq <- cut(merge_all$iraq_all, 
                      breaks=c(-1,100,200,400,700,1000,1550), 
                      labels=c("100","200", "400", "700","1000","1500"))
merge_all$b_so <- cut(merge_all$somalia_all, 
                      breaks=c(-1,100,200,400,700,1000,1550), 
                      labels=c("100","200", "400", "700","1000","1500"))
merge_all$b_ch <- cut(merge_all$china_all, 
                      breaks=c(-1,100,250,500,750,1000,2000,3000), 
                      labels=c("100","250", "500", "750","1000","2000","3000"))
merge_all$b_er <- cut(merge_all$eritrea_all, 
                      breaks=c(-1,100,250,500,750,1000,2000,3000), 
                      labels=c("100","250", "500", "750","1000","2000","3000"))


###############################
###  check a few countries  ###
###############################


clist <- c("b_er", "b_iraq","b_so","b_af", "b_sy","b_iran","b_ch","b_et")
clist2 <- c("Residents from Eritrea", "Residents from Iraq","Residents from Somalia", "Residents from Afghanistan","Residents from Syria", "Residents from Iran", "Residents from China", "Residents from Ethiopia" )
for (i in clist) {
  myfilename <- sprintf("TEX/maps/%s_map.png",i)
  ggplot() +
    geom_polygon(data = merge_all, 
                 aes_string(x = "long", y = "lat", group = "group", fill = i), 
                 color = "white", size = 0.0001) + 
    ggtitle(clist2[match(c(i),clist)]) +
    scale_fill_manual(name="", values= colb2) +
    coord_map() +
    theme(axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), panel.background= element_blank(), axis.title= element_blank()) +
    theme(plot.title= element_text(family="Arial",size = 20, face = "bold")) +
    theme(legend.title=element_text(size=20) , legend.text=element_text(size=20)) +
    theme(legend.key.size = unit(1, "cm"))
ggsave(myfilename, width = 6, height = 4)
}

#####################
###  Fig 1: maps  ###
#####################

t= list(scale_fill_manual(name="", values= colb2),coord_map(),theme(plot.title= element_text(size = 20, face = "bold")),    
  theme(axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), panel.background= element_blank(), axis.title= element_blank()),
  theme(legend.title=element_text(size=20) , legend.text=element_text(size=20)),theme(legend.key.size = unit(1, "cm")) )

A <- ggplot() +
    geom_polygon(data = merge_all, aes(x = long, y = lat, group = group, fill = b_iraq), 
    color = "white", size = 0.01) + ggtitle("Residents from Iraq") + t
print(A)
B <- ggplot() + geom_polygon(data = merge_all, aes(x = long, y = lat, group = group, fill = b_so), 
    color = "white", size = 0.01) + ggtitle("Residents from Somalia") + t
print(B)
  
pdf("TEX/graphs/maps.pdf",width = 10,height=12)
grid.arrange(A,B, layout_matrix = rbind(c(1),c(2)))
dev.off()


